package backend.alloctor;

import backend.component.RISCVfunction;
import backend.component.RISCVmodule;

import java.util.HashMap;


public class Alloctor {
    // RISCV模块，用于表示RISC-V的汇编模块
    private final RISCVmodule module;
    // 图着色寄存器分配器，用于寄存器分配
    private final GraphColoring_RegisterAllocator allocator;
    // 存储函数的堆栈大小
    private final HashMap<RISCVfunction, Integer> funcStack = new HashMap<>();

    // 构造函数，初始化相关组件
    public Alloctor(RISCVmodule module) {
        this.module = module;
        this.allocator = new GraphColoring_RegisterAllocator(module, funcStack);
    }

    // 执行寄存器分配和后处理的操作
    public void run() {
        for (RISCVfunction functions : module.getFunctions()) {
            getNeedChangeDes(functions);
        }
        allocator.run();
    }

    // 获取并记录函数的堆栈大小
    private void getNeedChangeDes(RISCVfunction objFunction) {
        int stackSize = objFunction.getStacksize();
        funcStack.put(objFunction, stackSize);
    }
}
